package com.example.demo.controller;


import com.example.demo.config.AjaxResult;
import com.example.demo.model.OrderInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.CommentService;
import com.example.demo.service.OrderService;
import com.example.demo.service.UserService;
import com.example.demo.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.cms.CMSObjectIdentifiers.data;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private CommentService commentService;


    @RequestMapping("/create")
    public Object createOrder(HttpServletRequest request,int pid,int quantity,int unitPrice,String traceNo) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，创建订单失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        int totalPrice = quantity * unitPrice;
        commentService.createComment(uid, pid, traceNo, totalPrice);
        return orderService.createOrder(uid, pid, quantity, unitPrice,traceNo,totalPrice);
    }

    @RequestMapping("/get-order")
    public Object getOrder(HttpServletRequest request,String state) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，获取订单失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        return switch (state) {
            case "all" -> AjaxResult.success("获取订单成功！", orderService.getAllOrder(uid));
            case "unpaid" -> {
                String status = "未支付";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus(uid, status));
            }
            case "paid" -> {
                String status = "已支付";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus(uid, status));
            }
            case "unreceived" -> {
                String status = "待收货";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus(uid, status));
            }
            case "received" -> {
                String status = "已收货";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus(uid, status));
            }
            case "uncommented" -> {
                String status = "待评价";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus1(uid, status));
            }
            case "commented" -> {
                String status = "已评价";
                yield AjaxResult.success("获取订单成功！", orderService.getOrderByStatus1(uid, status));
            }
            default -> AjaxResult.fail(-1, "获取订单失败！");
        };

    }

    @RequestMapping("/get-order-by-id")
    public Object getOrderByID(HttpServletRequest request,int pid,String traceNo) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，获取订单失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        return AjaxResult.success("获取订单成功！", orderService.getOrderById(uid, pid, traceNo));

    }

}
